package com.sq.shortlink.project.toolkit;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.sq.shortlink.project.common.constant.ShortLinkConstant;
import jakarta.servlet.http.HttpServletRequest;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Optional;

/**
 * 短链接工具类
 * @author quan
 */
public class LinkUtil {
    /**
     * 获取短链接过期时间戳
     * @param validDate 短链接过期时间
     * @return 短链接过期时间戳
     */
    public static long getLinkValidTime(LocalDateTime validDate){
        return Optional.ofNullable(validDate).map(each ->
             DateUtil.between(new Date(), Date.from(each.toInstant(ZoneOffset.ofHours(8))), DateUnit.MS)
        ).orElse(ShortLinkConstant.DEFAULT_CACHE_VALID_TIME);
    }


    /**
     * 获取操作系统名称
     * @param request Http请求对象
     * @return 操作系统名称
     */
    public static String getOperatingSystem(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null) {
            userAgent = userAgent.toLowerCase();
            if (userAgent.contains("windows")) {
                return "Windows";
            } else if (userAgent.contains("mac")) {
                return "Macintosh";
            } else if (userAgent.contains("linux")) {
                return "Linux";
            } else if (userAgent.contains("android")) {
                return "Android";
            } else if (userAgent.contains("iphone") || userAgent.contains("ipad")) {
                return "iOS";
            }
        }
        return "Unknown";
    }

    /**
     * 获取浏览器名称
     * @param request Http请求对象
     * @return 浏览器名称
     */
    public static String getBrowser(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");

        if (userAgent != null) {
            userAgent = userAgent.toLowerCase();
            if (userAgent.contains("msie") || userAgent.contains("trident")) {
                return "Internet Explorer";
            } else if (userAgent.contains("edg")) {
                return "Microsoft Edge";
            } else if (userAgent.contains("chrome")) {
                return "Google Chrome";
            } else if (userAgent.contains("safari") && !userAgent.contains("chrome")) {
                return "Safari";
            } else if (userAgent.contains("firefox")) {
                return "Mozilla Firefox";
            }
        }
        return "Unknown";
    }

    /**
     * 获取用户访问网络
     *
     * @param request 请求
     * @return 访问设备
     */
    public static String getNetwork(HttpServletRequest request) {
        String actualIp = ClientIPExtractor.getClientIP(request);
        // 这里简单判断IP地址范围，您可能需要更复杂的逻辑
        // 例如，通过调用IP地址库或调用第三方服务来判断网络类型
        return actualIp.startsWith("192.168.") || actualIp.startsWith("10.") ? "WIFI" : "Mobile";
    }

    /**
     * 获取用户访问设备
     *
     * @param request 请求
     * @return 访问设备
     */
    public static String getDevice(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("mobile")) {
            return "Mobile";
        }
        return "PC";
    }
}
